﻿/*
    Copyright (C) <2007-2013> Kay Diefenthal

    SimpleRadio is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SimpleRadio is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SimpleRadio.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace SimpleRadio
{
    /// <summary>
    /// <see cref="Utils"/> Class
    /// </summary>
    public static class Utils
    {
        #region Methods

        /// <summary>
        /// Retrieve the Countries
        /// </summary>
        /// <param name="path">path to the ServiceList <seealso cref="System.String"/></param>
        /// <returns>a List with Countries</returns>
        public static IList RetrieveCountries(String path)
        {
            var values = new List<string>();
            var document = XDocument.Load(path);
            var items = from item in document.Descendants("Service")
                        orderby item.Value
                        select new
                        {
                            item.Element("Country").Value
                        };
            foreach (var item in items)
            {
                if (!values.Contains(item.Value))
                    values.Add(item.Value);

            }
            return values;
        }

        /// <summary>
        /// Retrieve the Regions
        /// </summary>
        /// <param name="path">path to the ServiceList <seealso cref="System.String"/></param>
        /// <param name="country">Chosen country <seealso cref="System.String"/></param>
        /// <returns>a List with Regions</returns>
        public static IList RetrieveRegionforServices(String path, String country)
        {
            var values = new List<string>();
            var document = XDocument.Load(path);
            var items = from item in document.Descendants("Service")
                        where item.Element("Country").Value != null && item.Element("Country").Value == country
                        from r in item.Elements("Region")
                        orderby r.Value
                        select r;

            foreach (var item in items)
            {
                if (!values.Contains(item.Value))
                    values.Add(item.Value);
            }
            return values;
        }

        /// <summary>
        /// Retrieve the Genres
        /// </summary>
        /// <param name="path">path to the ServiceList <seealso cref="System.String"/></param>
        /// <param name="country">Chosen country <seealso cref="System.String"/></param>
        /// <returns>a List with Genre</returns>
        public static IList RetrieveGenreforServices(String path, String country)
        {
            var values = new List<string>();
            var document = XDocument.Load(path);
            var items = from item in document.Descendants("Service")
                        where item.Element("Country").Value != null && item.Element("Country").Value == country
                        from g in item.Elements("Genre")
                        orderby g.Value
                        select g;

            foreach (var item in items)
            {
                if (!values.Contains(item.Value))
                    values.Add(item.Value);
            }
            return values;
        }

        /// <summary>
        /// Retrieve Genre Sorted Service from List
        /// </summary>
        /// <param name="path">path to the ServiceList <seealso cref="System.String"/></param>
        /// <param name="country">Chosen country <seealso cref="System.String"/></param>
        /// <param name="value">Chosen genre <seealso cref="System.String"/></param>
        /// <returns>a Genre Sorted Services</returns>
        public static IList<Service> RetrieveGenreSortServices(String path, String country, String value)
        {
            var document = XDocument.Load(path);
            if (string.IsNullOrEmpty(value))
            {
                var items = from item in document.Descendants("Service")
                            where item.Element("Country").Value != null && item.Element("Country").Value == country
                            orderby item.Value
                            select new Service
                            {
                                DisplayName = item.Element("DisplayName").Value,
                                MediaPath = item.Element("MediaPath").Value,
                                LogoPath = item.Element("LogoPath").Value,
                                Country = item.Element("Country").Value,
                                Region = item.Element("Region").Value,
                                Genres = (from g in item.Elements("Genre")
                                          orderby g.Value
                                          select g.Value).ToList(),
                                IsFavorite = Convert.ToBoolean(item.Element("IsFavorite").Value)
                            };
                return items.ToList();
            }
            else
            {
                var items = from item in document.Descendants("Service")
                            where item.Element("Country").Value != null && item.Element("Country").Value == country
                            from genre in item.Elements("Genre")
                            where genre.Value == value
                            select new Service
                            {
                                DisplayName = item.Element("DisplayName").Value,
                                MediaPath = item.Element("MediaPath").Value,
                                LogoPath = item.Element("LogoPath").Value,
                                Country = item.Element("Country").Value,
                                Region = item.Element("Region").Value,
                                Genres = (from g in item.Elements("Genre")
                                          orderby g.Value
                                          select g.Value).ToList(),
                                IsFavorite = Convert.ToBoolean(item.Element("IsFavorite").Value)
                            };
                return items.ToList();
            }
        }

        /// <summary>
        /// Retrieve Region Sorted Service from List
        /// </summary>
        /// <param name="path">The Path to the ServiceList <seealso cref="System.String"/></param>
        /// <param name="country">The Chosen Country <seealso cref="System.String"/></param>
        /// <param name="region">the Chosen Region <seealso cref="System.String"/></param>
        /// <returns>a Region Sorted Services</returns>
        public static IList<Service> RetrieveRegionSortServices(String path, String country, String region)
        {
            var document = XDocument.Load(path);
            var items = from item in document.Descendants("Service")
                        where item.Element("Country").Value != null && item.Element("Country").Value == country
                        && item.Element("Region").Value != null && item.Element("Region").Value == region
                        select new Service
                        {
                            DisplayName = item.Element("DisplayName").Value,
                            MediaPath = item.Element("MediaPath").Value,
                            LogoPath = item.Element("LogoPath").Value,
                            Country = item.Element("Country").Value,
                            Region = item.Element("Region").Value,
                            Genres = (from g in item.Elements("Genre")
                                      orderby g.Value
                                      select g.Value).ToList(),
                            IsFavorite = Convert.ToBoolean(item.Element("IsFavorite").Value)
                        };
            return items.ToList();
        }

        #endregion
    }
}
